//
//  SSLDBManager.m
//  HD_SSL_FMDB
//
//  Created by 恒达的mini on 2017/5/24.
//  Copyright © 2017年 孙世龙. All rights reserved.
//

#import <objc/runtime.h>
#import "SSLDBManager.h"
#import "SSLDBModel.h"

#define DBName       @"GuiZhou.sqlite"            //数据库名字可以自定义

@interface SSLDBManager ()

@property (nonatomic,retain) FMDatabaseQueue *dbQueue;

@end

@implementation SSLDBManager

static  SSLDBManager *_instance = nil;

#pragma mark --------------------- 单例 -----------
+(instancetype)shareInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[super allocWithZone:NULL] init];
    });
    return _instance;
}
+(id)allocWithZone:(struct _NSZone *)zone{
    return [SSLDBManager shareInstance];
}
- (id)copyWithZone:(struct _NSZone *)zone{
    return [SSLDBManager shareInstance];
}

#pragma mark ---------------------- 懒加载 ----------
#pragma mark ----- 数据库路径
+(NSString *)dbPath{
    return [self dbPathWithDirectoryName:nil];
}
#pragma mark ----- 初始化
- (FMDatabaseQueue*)dbQueue{
    if (_dbQueue==nil) {
        _dbQueue = [[FMDatabaseQueue alloc] initWithPath:[self.class dbPath]];
    }
    return _dbQueue;
}

#pragma mark ----- 创建数据库，传参为空时，给定默认数据库名字
+(NSString *)dbPathWithDirectoryName:(NSString*)directoryName{
    NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (directoryName==nil||directoryName.length==0) {
        documentDir = [documentDir stringByAppendingPathComponent:@"SSLDB"];
    }else{
        documentDir = [documentDir stringByAppendingPathComponent:directoryName];
    }
    BOOL isDirectory;
    BOOL ret = [fileManager fileExistsAtPath:documentDir isDirectory:&isDirectory];
    if (!ret||!isDirectory) {
        [fileManager createDirectoryAtPath:documentDir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *dbpath = [documentDir stringByAppendingPathComponent:DBName];
    
    return dbpath;
}

#pragma mark ----- 新建数据库
- (BOOL)changeDBWithDirectoryName:(NSString *)DBname{
    if (_instance.dbQueue) {
        _instance.dbQueue = nil;
    }
    _instance.dbQueue = [[FMDatabaseQueue alloc] initWithPath:[SSLDBManager dbPathWithDirectoryName:DBname]];
    
    int numClasses;
    Class *classes = NULL;
    numClasses = objc_getClassList(NULL, 0);
    
    if (numClasses>0) {
        classes = (__unsafe_unretained Class *)malloc(sizeof(Class) *numClasses);
        numClasses = objc_getClassList(classes, numClasses);
        
        for (int i=0; i<numClasses; i++) {
            if (class_getSuperclass(classes[i]) == [SSLDBModel class]) {
                id class = classes[i];
                [class performSelector:@selector(createTable) withObject:nil afterDelay:0.0];
            }
        }
        free(classes);
    }
    
    return YES;
    
}

#if ! __has_feature(objc_arc)
- (oneway void)release{}
- (id)autorelease{
    return _instance;
}
- (NSUInteger)retainCount{
    return 1;
}
#endif

@end
